<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <el-select v-model="queryParams.priority" placeholder="请选择优先级" clearable>
          <el-option 
            v-for="dict in cases_priority_level" 
            :key="dict.value" 
            :label="dict.label" 
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="assignee">
        <el-input
          v-model="queryParams.assignee"
          placeholder="请输入负责人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['command:cases:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['command:cases:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['command:cases:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['command:cases:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表部分 height="800"-->
    <el-table v-loading="loading" :data="casesList"  border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="缺陷ID" align="center" prop="id" /> -->
      <el-table-column fiexd="left" width="120" label="所属项目" align="left" prop="projectName" />
      <el-table-column fiexd="left" label="标题" align="left" prop="title" />
      <!-- <el-table-column label="描述" align="center" prop="description" /> -->
      <!-- <el-table-column label="重现步骤" align="center" prop="stepsToReproduce" /> -->
      <el-table-column label="优先级" width="100" align="center" prop="priority" 
        :filters="filterData" 
        :filter-method="filterHandler"
      >
        <template #default="scope">
          <dict-tag :options="cases_priority_level" :value="scope.row.priority"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="cases_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="负责人" width="100" align="center" prop="assignee" />
      <el-table-column label="创建人" width="100" align="center" prop="createBy" />
      <el-table-column label="创建时间" sortable align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>  <!-- '{y}-{m}-{d}' -->
        </template>
      </el-table-column>

      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['command:cases:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['command:cases:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改缺陷用例对话框 -->
    <el-dialog :title="title" v-model="open" width="1080px" append-to-body>
      <el-form ref="casesRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <!-- <el-input v-model="form.assignee" placeholder="请输入负责人" /> -->
          <el-select v-model="form.projectId" placeholder="请输入项目名称">
            <el-option 
              v-for="dict in projectList"
              :key="dict.projectId"
              :label="dict.projectName" 
              :value="dict.projectId" 
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" :rows="6" />
        </el-form-item>
        <el-form-item label="重现步骤" prop="stepsToReproduce">
          <el-input v-model="form.stepsToReproduce" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <!-- <el-input v-model="form.priority" placeholder="请输入优先级" /> -->
          <el-select v-model="form.priority" placeholder="请选择优先级">
            <el-option
              v-for="dict in cases_priority_level"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <!-- <el-input v-model="form.priority" placeholder="请输入优先级" /> -->
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in cases_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="负责人" prop="assignee">
          <!-- <el-input v-model="form.assignee" placeholder="请输入负责人" /> -->
          <el-select v-model="form.assignee" placeholder="请输入负责人">
            <el-option 
              v-for="dict in userList"
              :key="dict.userId"
              :label="dict.nickName" 
              :value="dict.nickName" 
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Cases">
import { listCases, getCases, delCases, addCases, updateCases } from "@/api/command/cases";

import { getUser } from "../../../api/system/user";
import { computed } from "vue";
// 额外
import { listUser } from "@/api/system/user";
import { listProject, getProject, delProject, addProject, updateProject } from "@/api/command/project";

const { proxy } = getCurrentInstance();
const { cases_priority_level, cases_status } = proxy.useDict('cases_priority_level', 'cases_status');
// console.log(cases_priority_level,'weaw');

const filterData=computed(()=>cases_priority_level.value.map((x)=>({...x,text:x.label})))
console.log(filterData, "123123")

const casesList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
// 额外添加
const dateRange = ref([]);
const userList = ref([]);
const projectList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    description: null,
    stepsToReproduce: null,
    priority: null,
    status: null,
    createTime: null,
    createBy: null,
    assignee: null
  },
  rules: {
    title: [
      { required: true, message: "标题不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);


// 表格筛选 未完成
function filterHandler(value, row, column) {
  const priority = column['priority'];
  return row[priority] === value;
}

/** 查询用户列表 */
function getUserList() {
  loading.value = true;
  listUser(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    loading.value = false;
    userList.value = res.rows;
    total.value = res.total;
  });
};

/** 查询项目列表 */
function getProjectList() {
  loading.value = true;
  listProject(queryParams.value).then(response => {
    projectList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询缺陷用例列表 */
function getList() {
  loading.value = true;
  listCases(queryParams.value).then(response => {
    casesList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    description: null,
    stepsToReproduce: null,
    priority: null,
    status: null,
    createTime: null,
    createBy: null,
    assignee: null
  };
  proxy.resetForm("casesRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加缺陷用例";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getCases(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改缺陷用例";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["casesRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCases(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCases(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除缺陷用例编号为"' + _ids + '"的数据项？').then(function() {
    return delCases(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('command/cases/export', {
    ...queryParams.value
  }, `cases_${new Date().getTime()}.xlsx`)
}

getList();
getUserList();
getProjectList();
</script>
